// pages/myprofile/myprofile.js
var request = require("../../../utils/request.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: [],
    sexArr:["未知","男","女"],
    modalFlag : false,
    modal:{
      title:"请输入昵称",
      input:'nickname',
      value:''
    },
    labels:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = wx.getStorageSync("userInfo");
    // app.globalData.userInfo  
    var that = this;
    this.setData({
      userInfo:userInfo
    })

    this.init();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 修改用户头像
  openView:function(e){
    var that = this;
    var name = e.currentTarget.dataset.name;
    switch(name){
      case "avatar":
        that.uploadFile();
      break;
      case "nickname":
        that.setData({
          "modalFlag": true,
          "modal.title":"请输入昵称",
          "modal.input": "nickname",
          "modal.value": that.data.userInfo.Name
        });
      break;
    }
  },
  //上传头像
  uploadFile:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.chooseImage({
      success: function (res) {
        
        const tempFilePaths = res.tempFilePaths;

        wx.navigateTo({
          url: '../../upload/upload?src=' + tempFilePaths
        })

        // wx.uploadFile({
        //   url: 'https://weike.qclub-online.com/api/Customer/EditAvatar',
        //   filePath: tempFilePaths[0],
        //   name: 'file',
        //   success(res) {
        //     wx.hideLoading();
        //     const data = (JSON.parse(res.data));
        //     console.log(data.data);
        //     that.setData({
        //       "userInfo.AvatarUrl":"http://qbonbon.qclub-online.com/_Document/Image/" + data.data
        //     })
        //   }
        // })
      },
    })
  },

  // 获取表单值
  getInput:function(e){
    console.log(e.detail.value);
    var field = e.currentTarget.dataset.field;
    var str = "userInfo."+field;
    console.log(str);
    this.setData({
      [str]:e.detail.value
    })
  },
  //取消按钮
  btncCancel:function(e){
    this.setData({
      modalFlag: false
    }) 
  },
  submit:function(){
    var that = this;
    var data = that.data.userInfo;

    var labels = that.data.labels;
    var hobby = '';
    for(var i in labels){
      if (labels[i]['show']){
        var label = labels[i]['ID']+","
        hobby += label
      }
    }
    hobby = hobby.substring(0, hobby.length - 1)
    data['Hobby'] = hobby;

    console.log('111',data);
    request.request("api/Customer/EditInfo",data,"POST",1).then(function(res){
      console.log(res);
      if(res.data.status == 1){
        app.updateUserInfo();
        var pages = getCurrentPages();
        var prevpage = pages[pages.length - 2];
        console.log(prevpage)
        app.globalData.userInfo = that.data.userInfo;
        prevpage.onLoad()
        wx.navigateBack()
      }
    })
  },
  //init初始化
  init:function(){
    var that = this;
    request.request("api/CourseLabel/GetCourseLabels").then(function(res){
      var data = res.data.data;

      var userInfo = wx.getStorageSync("userInfo");
      var hobby = userInfo['Hobby'].split(",")

      console.log(hobby)
      for(var i in data){
        console.log(data[i]['ID']);
        if (hobby.indexOf(data[i]['ID']+"") > -1 ){
          data[i]['show'] = true
        }
        else{
          data[i]['show'] = false
        }
      }

      console.log(data)

      that.setData({
        labels:data
      })
    })
  },
  //获取兴趣爱好
  getMyHobby:function(e){
    var index = e.currentTarget.dataset.index;
    var that = this;
    var labels = that.data.labels;
    labels[index]['show'] = labels[index]['show'] ? false : true;
    var str = "labels["+index+"].show";
    that.setData({
      [str]: labels[index]['show']
    })
  },

  //获取手机号
  getPhone:function(e){
    console.log(e);
  }

})